<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryForm"
      size="small"
      :inline="true"
      v-show="showSearch"
      label-width="98px"
    >
      <el-form-item label="新闻稿发布者" prop="noticeAuthor">
        <el-input
          v-model="queryParams.noticeAuthor"
          placeholder="请输入新闻稿发布者"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="新闻稿标题" prop="noticeTitle">
        <el-input
          v-model="queryParams.noticeTitle"
          placeholder="请输入新闻稿标题"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <!-- <el-form-item label="审核状态" prop="noticeState">
        <el-input
          v-model="queryParams.noticeState"
          placeholder="请输入0-待审核，1-审核未通过，2-审核通过"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item> -->
      <el-form-item>
        <el-button
          type="primary"
          icon="el-icon-search"
          size="mini"
          @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
          >重置</el-button
        >
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['sportnotice:notice:add']"
          >新增</el-button
        >
      </el-col>
      <!-- <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['sportnotice:notice:edit']"
          >修改</el-button
        >
      </el-col> -->
      <!-- <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['sportnotice:notice:remove']"
          >删除</el-button
        >
      </el-col> -->
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['sportnotice:notice:export']"
          >导出</el-button
        >
      </el-col>
      <right-toolbar
        :showSearch.sync="showSearch"
        @queryTable="getList"
      ></right-toolbar>
    </el-row>

    <el-table
      v-loading="loading"
      :data="noticeList"
      @selection-change="handleSelectionChange"
    >
      <!-- <el-table-column type="selection" width="55" align="center" /> -->
      <!-- <el-table-column label="新闻稿uid" align="center" prop="noticeUuid" /> -->
      <el-table-column
        label="新闻稿发布者"
        align="center"
        prop="noticeAuthor"
        width="120px"
      />
      <el-table-column
        label="新闻稿标题"
        align="center"
        prop="noticeTitle"
        width="130px"
      />
      <el-table-column
        label="审核状态"
        align="center"
        prop="noticeState"
        width="149px"
      >
        <template slot-scope="scope">
          <span v-if="scope.row.noticeState == 0">
            <el-button type="primary" :loading="true" round>待审核</el-button>
          </span>
          <span v-if="scope.row.noticeState == 1">
            <el-button type="danger" round>审核未通过</el-button>
          </span>
          <span v-if="scope.row.noticeState == 2">
            <el-button type="success" round>审核通过</el-button>
          </span>
        </template>
      </el-table-column>

      <el-table-column
        label="新闻稿内容"
        align="center"
        prop="noticeContent"
        width="710px"
      />
      <el-table-column
        label="备注"
        align="center"
        prop="remark"
        width="200px"
      />
      <el-table-column
        label="操作"
        align="center"
        class-name="small-padding fixed-width"
      >
        <template slot-scope="scope">
          <span
            v-show="
              (scope.row.noticeState != 2 && scope.row.noticeState != 1) ||
              user.userName == 'admin'
            "
          >
            <el-button
              size="mini"
              type="text"
              icon="el-icon-edit"
              @click="handleUpdate(scope.row)"
              v-hasPermi="['sportnotice:notice:edit']"
              >修改</el-button
            >
          </span>

          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handlequerylist(scope.row)"
            >查看</el-button
          >

          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['sportnotice:notice:remove']"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改通知公告对话框 -->
    <el-dialog
      :title="title"
      :visible.sync="open"
      width="1250px"
      height="999px"
      append-to-body
    >
      <el-form ref="form" :model="form" :rules="rules" label-width="99px">
        <el-form-item label="新闻稿发布者" prop="noticeAuthor">
          <el-input
            :disabled="true"
            v-model="user.userName"
            placeholder="请输入新闻稿发布者"
          />
        </el-form-item>
        <el-form-item label="新闻稿标题" prop="noticeTitle">
          <el-input v-model="form.noticeTitle" placeholder="请输入新闻稿标题" />
        </el-form-item>
        <!-- <el-form-item label="审核状态" prop="noticeState">
          <el-input v-model="form.noticeState" placeholder="请输入0-待审核，1-审核未通过，2-审核通过" />
        </el-form-item> -->

        <span v-show="user.userName == 'admin'">
          <el-form-item label="审核状态" prop="noticeState">
            <el-select v-model="form.noticeState" placeholder="请选择审核状态">
              <el-option label="审核未通过" value="1" />
              <el-option label="待审核" value="0" />
              <el-option label="审核通过" value="2" />
            </el-select>
          </el-form-item>
        </span>

        <el-form-item label="新闻稿内容">
          <editor v-model="form.noticeContent" :min-height="192" />
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" placeholder="请输入备注" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

    <!-- 查看通知公告对话框 -->
    <el-dialog
      :title="title"
      :visible.sync="queryopen"
      width="1250px"
      height="999px"
      append-to-body
    >
      <el-form ref="form" :model="form" :rules="rules" label-width="99px">
        <el-form-item label="新闻稿发布者" prop="noticeAuthor">
          <el-input
            :disabled="true"
            v-model="user.userName"
            placeholder="请输入新闻稿发布者"
          />
        </el-form-item>
        <el-form-item label="新闻稿标题" prop="noticeTitle">
          <el-input v-model="form.noticeTitle" placeholder="请输入新闻稿标题" />
        </el-form-item>
        <!-- <el-form-item label="审核状态" prop="noticeState">
          <el-input v-model="form.noticeState" placeholder="请输入0-待审核，1-审核未通过，2-审核通过" />
        </el-form-item> -->

        <span v-show="user.userName == 'admin'">
          <el-form-item label="审核状态" prop="noticeState">
            <el-select v-model="form.noticeState" placeholder="请选择审核状态">
              <el-option label="审核未通过" value="1" />
              <el-option label="待审核" value="0" />
              <el-option label="审核通过" value="2" />
            </el-select>
          </el-form-item>
        </span>

        <el-form-item label="新闻稿内容">
          <editor v-model="form.noticeContent" :min-height="192" />
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" placeholder="请输入备注" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <!-- <el-button type="primary" @click="submitForm">确 定</el-button> -->
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { getUserProfile } from "@/api/system/user"; //引入当前用户admin
import {
  listNotice,
  getNotice,
  delNotice,
  addNotice,
  updateNotice,
  querybouncedzt,
} from "@/api/sportnotice/notice";

export default {
  name: "Notice",
  data() {
    return {
      //管理员权限
      user: {},
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 通知公告表格数据
      noticeList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      queryopen: false, //查看数据
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        noticeAuthor: null,
        noticeTitle: null,
        noticeState: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {},
    };
  },
  created() {
    this.getList();
    this.getUser(); //用户账户
  },
  methods: {
    /** 查询通知公告列表 */
    getList() {
      this.loading = true;
      listNotice(this.queryParams).then((response) => {
        this.noticeList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
       
    },

    //调用admin 登录时  审核权限生成
    getUser() {
      getUserProfile().then((response) => {
        this.user = response.data;
        this.roleGroup = response.roleGroup;
        this.postGroup = response.postGroup;
      });
    },

    // 取消按钮
    cancel() {
      this.open = false;
      this.queryopen = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        noticeUuid: null,
        noticeAuthor: null,
        noticeTitle: null,
        noticeState: null,
        noticeContent: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        remark: null,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.noticeUuid);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加通知公告";
    },

    /** 查看按钮操作 */
    handlequerylist(row) {
      this.reset();
      const noticeUuid = row.noticeUuid;
      getNotice(noticeUuid).then((response) => {
        this.form = response.data;
        (this.queryopen = true), //查看数据 = true;
          (this.title = "查看通知公告");
        // this.submitForm().prevent()
      });
    },

    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const noticeUuid = row.noticeUuid || this.ids;
      getNotice(noticeUuid).then((response) => {
        this.form = response.data;
        this.open = true;
        this.title = "修改通知公告";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          if (this.form.noticeUuid != null) {
            // if (this.form.noticeState != 2) {
            updateNotice(this.form).then((response) => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
            //   this.$modal.msgError("审核成功的新闻稿,不可以删除哦");
            // } else {
          } else {
            addNotice(this.form).then((response) => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const noticeTitles = row.noticeTitle;
      const noticeUuids = row.noticeUuid || this.ids;

      if (this.user.userName != "admin" && row.noticeState == 2) {
        this.$message({
          showClose: true,
          message: "审核成功的数据不能删除此条信息,请联系管理员",
          type: "error",
        });
      } else {
        this.$modal
          .confirm('是否确认删除通知公告标题为"' + noticeTitles + '"的数据项？')
          .then(function () {
            return delNotice(noticeUuids);
          })
          .then(() => {
            this.getList();
            this.$modal.msgSuccess("删除成功");
          })
          .catch(() => {});
      }
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download(
        "sportnotice/notice/export",
        {
          ...this.queryParams,
        },
        `notice_${new Date().getTime()}.xlsx`
      );
    },
  },
};
</script>

<style scoped>
</style>
